﻿<html>
<head>
	<title>基数排序</title>
</head>
<body>
	<script language="JavaScript"> 
	function radixSort(arr,maxDigit){
		var mod=10;
		var dev=1;
		var counter=[];
		for(var i=0;i<maxDigit;i++,dev*=10,mod*=10){
			for(var j=0;j<arr.length;j++){
				var bucket=parseInt((arr[j]%mod)/dev);
				if(counter[bucket]==null){
					counter[bucket]=[];
				}
				counter[bucket].push(arr[j]);
			}
			var pos=0;
			for(var j=0;j<counter.length;j++){
				var value=null;
				if(counter[j]!=null){
					while((value=counter[j].shift())!=null){
						arr[pos++]=value;
					}
				}
			}
		}
		return arr;
	}
	var arr=[];
	for(var i = 0; i < 10; ++i){
		arr[i] = Math.floor(Math.random()*100+1);
	}
	console.log("原始数组："+arr.toString());
	var array = radixSort(arr,2);
	console.log("排序后的数组："+array.toString());
</script>
</body>
</html>
